package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.commons.service.CommonsService;
import cc.linker.steplink.crm.commons.utils.CharUtils;
import cc.linker.steplink.crm.merchant.service.GiftTaskService;
import cc.linker.steplink.crm.merchant.service.acs.MerchantInfoService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/gifttask")
public class GiftTaskController {

    @Autowired
    private UserInfoUtil userInfoUtil;
    @Autowired
    private GiftTaskService giftTaskService;
    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private CommonsService commonsService;


    /**
     * 获取列表
     * @param status
     * @param keywords
     * @param pageIndex
     * @param pageSize
     * @param startTime
     * @param endTime
     * @param request
     * @return
     */
    @GetMapping("/getlist")
    public PageInfo getList(
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "keywords", required = false) String keywords,
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
            @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
            HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        boolean ifSaler = userinfo.getSaler();

        Map<String,Object> map = new HashMap<>();
        if(!StringUtils.isEmpty(status)){
            map.put("status",status);
        }
        if(!StringUtils.isEmpty(startTime)){
            map.put("startTime",startTime+" 00:00:00");
        }
        if(!StringUtils.isEmpty(endTime)){
            map.put("endTime",endTime+" 23:59:59");
        }
        map.put("orgId", orgId);
        if (ifSaler) {
            //小b查看活动只展示自己的活动
            String customerId = merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId());
            map.put("createUser", customerId);
        }
        if(!StringUtils.isEmpty(keywords)){
            map.put("name", CharUtils.escapeQueryChars(keywords));
        }
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(giftTaskService.getList(map));
    }

    /**
     * 获取详情
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/getinfo")
    public Map<String,Object> getInfo(@RequestParam(value = "id") Integer id, HttpServletRequest request){
        Map<String,Object> param = new HashMap<>();
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        param.put("id",id);
        return giftTaskService.getInfo(param);
    }

    /**
     * 新增/编辑
     * @param param
     * @param request
     */
    @PostMapping("/save")
    public void save(@RequestBody Map<String, Object> param, HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        String customerId = orgId;
        if (userinfo.getSaler()) {
            //小b查看活动只展示自己的活动
            customerId = merchantInfoService.getCustomerIdByAdminId(userinfo.getAdminId());
        }
        param.put("createUser", customerId);
        giftTaskService.save(param);
    }

    /**
     * 删除
     * @param param
     * @param request
     */
    @PostMapping("/del")
    public void del(@RequestBody Map<String, Object> param, HttpServletRequest request){
        if(StringUtils.isEmpty(param.get("id"))){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        giftTaskService.del(param);
    }


    /**
     * 获取发放记录
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/getsendlist")
    public PageInfo getSendList(@RequestParam(value = "id") Integer id,
                                @RequestParam(value = "keywords", required = false) String keywords,
                                @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                                @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
                                @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
                                @RequestParam(value = "status", defaultValue = "", required = false) Integer status,
                                HttpServletRequest request){
        Map<String,Object> param = new HashMap<>();
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        param.put("id",id);
        if(!StringUtils.isEmpty(keywords)){
            param.put("keywords",CharUtils.escapeQueryChars(keywords));
        }
        if(!StringUtils.isEmpty(startTime)){
            param.put("startTime",startTime + " 00:00:00");
        }
        if(!StringUtils.isEmpty(endTime)){
            param.put("endTime",endTime + "23:59:59");
        }
//        if(!StringUtils.isEmpty(status)){
//            param.put("status",status);
//        }
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(giftTaskService.getSendList(param));
    }

    /**
     * 导出发放记录
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/exportsendlist")
    public void exportSendList(@RequestParam(value = "id") Integer id,
                                   @RequestParam(value = "keywords", required = false) String keywords,
                                   @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                                   @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                   @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
                                   @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
                                   @RequestParam(value = "status", defaultValue = "", required = false) Integer status,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        Map<String,Object> param = new HashMap<>();
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        param.put("id",id);
        if(!StringUtils.isEmpty(keywords)){
            param.put("keywords",CharUtils.escapeQueryChars(keywords));
        }
        if(!StringUtils.isEmpty(startTime)){
            param.put("startTime",startTime + " 00:00:00");
        }
        if(!StringUtils.isEmpty(endTime)){
            param.put("endTime",endTime + "23:59:59");
        }
//        if(!StringUtils.isEmpty(status)){
//            param.put("status",status);
//        }

        Calendar cal = Calendar.getInstance();
        int y = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH) + 1;
        String dayStr = ""+day;
        if(day<10){
            dayStr = "0"+day;
        }
        String monthStr = ""+month;
        if(month<10){
            monthStr = "0"+month;
        }

        List<Map<String, Object>> sendList = giftTaskService.getSendList(param);
        for(Map<String, Object> da:sendList){
            da.put("createTime",da.get("createTime").toString().replace(".0",""));
        }
        String[] key = {"customerName","bindPhone","createTime","content","num"};
        String[] title = {"用户","手机号","发放时间","礼包内容","卡券数量"};
        commonsService.excelDownload(sendList,key,title,"生日礼包发放详情"+y+monthStr+dayStr,response);
    }

}
